package com.hn.kiss.modules.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hn.kiss.config.common.CommonPage;
import com.hn.kiss.config.common.CommonResult;
import com.hn.kiss.config.compoent.BaseController;
import com.hn.kiss.modules.sys.dto.RoleUserDTO;
import com.hn.kiss.modules.sys.dto.UserRoleDTO;
import com.hn.kiss.modules.sys.entity.Role;
import com.hn.kiss.modules.sys.service.IUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;


@Api(tags = "SysUserRoleController", description = "用户角色关联管理")
@RestController
@RequestMapping("/sys/user-role")
@Validated
public class UserRoleController extends BaseController {

    @Autowired
    IUserRoleService userRoleService;

    @ApiOperation("用户角色分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("/userRolePage")
    public CommonPage<UserRoleDTO> userRolePage(@NotNull Integer pageNo,
                                                @NotNull Integer pageSize,
                                                @NotBlank String userId){
        Page<Role> pageInfo = new Page<>(pageNo, pageSize);
        Page<UserRoleDTO> page = userRoleService.userRolePage(pageInfo, userId);
        return CommonPage.restPage(page);
    }

    @ApiOperation("非用户角色分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("/unUserRolePage")
    public CommonPage<UserRoleDTO> unUserRolePage(@NotNull Integer pageNo,
                                                  @NotNull Integer pageSize,
                                                  @NotBlank String userId){
        Page<UserRoleDTO> pageInfo = new Page<>(pageNo, pageSize);

        Page<UserRoleDTO> page = userRoleService.unUserRolePage(pageInfo, userId);
        return CommonPage.restPage(page);
    }

    @ApiOperation("角色用户分页列表分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "roleCode", value = "角色代码", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("/roleUserPage")
    public CommonPage<RoleUserDTO> roleUserPage(@NotNull Integer pageNo,
                                                @NotNull Integer pageSize,
                                                @NotBlank String roleCode){
        Page<RoleUserDTO> pageInfo = new Page<>(pageNo, pageSize);
        Page<RoleUserDTO> page = userRoleService.roleUserPage(pageInfo, roleCode);
        return CommonPage.restPage(page);
    }


    @ApiOperation("非角色用户分页列表分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "roleCode", value = "角色代码", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("/unRoleUserPage")
    public CommonPage<RoleUserDTO> unRoleUserPage(@NotNull Integer pageNo,
                                                  @NotNull Integer pageSize,
                                                  @NotBlank String roleCode){
        Page<RoleUserDTO> pageInfo = new Page<>(pageNo, pageSize);
        Page<RoleUserDTO> page = userRoleService.unRoleUserPage(pageInfo, roleCode);
        return CommonPage.restPage(page);
    }


    @ApiOperation("给角色添加用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rodeCode", value = "角色代码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userIds", value = "用户id(多个以','隔开)", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping("/addRoleUsers")
    public CommonResult addRoleUsers(@NotBlank String rodeCode, @NotBlank String userIds){
        String[] strs = userIds.split(",");
        boolean b = userRoleService.addRoleUsers(rodeCode, strs);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }


    @ApiOperation("给用户添加角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "rodeCodes", value = "角色代码(多个以','隔开)", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping("/addUserRoles")
    public CommonResult addUserRoles(@NotBlank String userId, @NotBlank String rodeCodes){
        String[] strs = rodeCodes.split(",");

        boolean b = userRoleService.addUserRoles(userId, strs);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }


    @ApiOperation("删除用户角色关联")
    @ApiImplicitParam(name = "ids", value = "ids(多个以','隔开)", required = true, dataType = "String", paramType = "query")
    @DeleteMapping("/delete")
    public CommonResult removeByPk(@NotBlank String ids){
        String[] split = ids.split(",");
        List<String> strings = Arrays.asList(split);
        boolean b = userRoleService.removeByIds(strings);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("设置默认角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "rodeCode", value = "角色代码", required = true, dataType = "String", paramType = "query")
    })
    @PatchMapping("/defaultRole")
    public CommonResult defaultRole(@NotBlank String userId, @NotBlank String rodeCode){
        boolean b = userRoleService.setDefaultRole(userId, rodeCode);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }





}
